package com.ktjy.ch02.controller;

import com.ktjy.ch02.entity.User;
import com.ktjy.ch02.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController extends BaseController{

    @Autowired
    UserService userService;

    @RequestMapping("/findAll")
    public List findAll(){
        return userService.findAll();
    }
    @RequestMapping("/insert")
    public int insert(User user){
        return userService.insert(user);
    }
    @RequestMapping("/update")
    public int update(User user){
        return userService.update(user);
    }
    @RequestMapping("/del/{id}")
    public int delete(@PathVariable Integer id) {
        if (id == null) {
            // 处理 id 为 null 的情况，例如抛出自定义异常或返回错误信息
            throw new IllegalArgumentException("ID cannot be null");
        }
        return userService.delete(id);
    }
    public int delete(@PathVariable Integer id) {
        if (id == null) {
            // 处理 id 为 null 的情况，例如抛出自定义异常或返回错误信息
            throw new IllegalArgumentException("ID cannot be null");
        }
        return userService.delete(id);
    }


}
